//
//  ThemeSwitchViewController.swift
//  Wanbair
//
//  Created by Li on 2025/3/7.
//

import UIKit

class ThemeSwitchViewController: ThemeBaseViewController {
    
    private let bgImageView: UIImageView = {
        let label = UIImageView()
        return label
    }()
    
    
    // 标题标签
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 24, weight: .bold)
        label.textAlignment = .center
        return label
    }()

    // 切换主题按钮
    private let switchButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Switch Theme", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        button.backgroundColor = .systemBlue
        button.setTitleColor(.white, for: .normal)
        button.layer.cornerRadius = 8
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "切换主题"
        
        setupUI()
        
        switchButton.addTarget(self, action: #selector(switchTheme), for: .touchUpInside)

    }

    // 设置 UI 布局
    private func setupUI() {
        view.addSubview(bgImageView)
        view.addSubview(titleLabel)
        view.addSubview(switchButton)

        bgImageView.frame = view.bounds
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        switchButton.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            titleLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 130),

            switchButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            switchButton.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 40),
            switchButton.widthAnchor.constraint(equalToConstant: 200),
            switchButton.heightAnchor.constraint(equalToConstant: 50)
        ])
    }

    
    // 应用当前主题
    override func applyTheme() {
        let theme = ThemeManager.shared.currentTheme
        bgImageView.image = .themeImage(named: "bgImageView")
        view.backgroundColor = .themeColor(hex: theme.backgroundColor)
        titleLabel.textColor = .themeTextFieldColor
        titleLabel.text = "Current Theme: \(theme.name)"
        switchButton.backgroundColor = theme.colorFromHex(theme.buttonColor)
        
        // 设置导航栏背景色和文字颜色
        let navBgColor = theme.colorFromHex(theme.navBgColor)
        let navTextColor = theme.colorFromHex(theme.navTextColor)
        navigationController?.navigationBar.barTintColor = navBgColor
        navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: navTextColor
        ]
        
        // 设置状态栏样式
        setNeedsStatusBarAppearanceUpdate()
        
    }

    // 返回当前状态栏样式
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return ThemeManager.shared.currentTheme.statusBarStyleValue
    }
    
    
    // 切换主题
    @objc func switchTheme() {
        let themes = ThemeManager.shared.getAllThemes()
        let currentThemeIndex = themes.firstIndex { $0.name == ThemeManager.shared.currentTheme.name } ?? 0
        let nextThemeIndex = (currentThemeIndex + 1) % themes.count
        let nextTheme = themes[nextThemeIndex]
        ThemeManager.shared.applyTheme(named: nextTheme.name)
    }
    
    
}
